package wetalk.software.bupt.com.wetalk.view.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import wetalk.software.bupt.com.wetalk.DB.WeTalkDB;
import wetalk.software.bupt.com.wetalk.R;
import wetalk.software.bupt.com.wetalk.adapter.SearchListAdapter;
import wetalk.software.bupt.com.wetalk.model.po.ChatUser;
import wetalk.software.bupt.com.wetalk.model.po.SearchMessageRecord;

public class searchActivity extends BaseActivity implements OnItemClickListener,OnItemLongClickListener {
    SearchListAdapter searchListAdapter;
    EditText editText;
    ListView listView;
    Button button_search;
    SearchMessageRecord searchMessageRecord;
    String msg;
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        editText = (EditText)findViewById(R.id.msg_search);
        button_search = (Button)findViewById(R.id.button_search);
        listView = (ListView)findViewById(R.id.search_list);
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);
        button_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(editText.getText().toString().equals("")){
                    Toast.makeText(searchActivity.this, "请输入查询的内容", Toast.LENGTH_SHORT).show();
                }else if(WeTalkDB.create(searchActivity.this).queryMessageRecord(editText.getText().toString())==null){
                    Toast.makeText(searchActivity.this, "没有查询到相关内容", Toast.LENGTH_SHORT).show();
                    listView.setAdapter(null);
                }else{
                    searchListAdapter = new SearchListAdapter(searchActivity.this,R.layout.item_conversation, WeTalkDB.create(searchActivity.this).queryMessageRecord(editText.getText().toString()));
                    Log.i("searchActivity","查询成功");
                    msg = editText.getText().toString();
                    listView.setAdapter(searchListAdapter);
                }
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        searchMessageRecord = searchListAdapter.getItem(position);
        ChatUser user = new ChatUser();
        user.setUserId(searchMessageRecord.getTargetid());
        user.setAvatar(searchMessageRecord.getAvatar());
        user.setNick(searchMessageRecord.getNick());
        user.setUserName(searchMessageRecord.getUserName());

        Intent intent = new Intent(searchActivity.this, searchActivity2.class);
        intent.putExtra("user",user);
        intent.putExtra("msg",msg);
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        return false;
    }
}
